/**
 * Copyright ©2016-2020 northkingbpo Corporation, All Rights Reserved
 * http://www.northkingbpo.net/
 */
package profile.hujia.simulation.data.service;

import profile.hujia.simulation.data.common.CommonFunction;
import profile.hujia.simulation.data.common.ExcelFile;
import profile.hujia.simulation.data.config.SystemConfig;
import profile.hujia.simulation.data.domain.Person;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * class info
 *
 * @author hujia
 * @date 2019-06-17 16:39
 */
public class CapacityData {
    private HashMap<Integer, Person> persons;
    private ExcelFile excelFile;

    public CapacityData(HashMap<Integer, Person> persons, ExcelFile excelFile) {
        this.persons = persons;
        this.excelFile = excelFile;
    }

    public void run() {
        //title
        StringBuilder title = new StringBuilder("操作员名称");
        for (int i = 0; i < SystemConfig.ROUND_NUMBER; ++i) {
            title.append(String.format(",第%02d轮", i + 1));
        }

        HashMap<Integer, List<String>> capacity = new HashMap<>();
        for (int i = 0; i < SystemConfig.TOTAL_PERSON_NUMBER; ++i) {
            capacity.put(i + 1, new ArrayList<>());
            capacity.get(i + 1).add(persons.get(i + 1).getPersonName());
        }
        for (int i = 0; i < SystemConfig.ROUND_NUMBER; ++i) {
            changeCapacity();
            for (int j = 0; j < SystemConfig.TOTAL_PERSON_NUMBER; ++j) {
                capacity.get(j + 1).add(persons.get(j + 1).getLabelMap().get("Capacity").toString());
            }
        }
        excelPersonValue("Capacity-改", title.toString(), capacity);
    }

    public void changeCapacity() {
        Integer changeNumber = (int) (SystemConfig.TOTAL_PERSON_NUMBER * SystemConfig.CHANGE_PERCENTAGE);
        List<Integer> numbers = CommonFunction.createUniqueRandomValues(changeNumber, 1, SystemConfig.TOTAL_PERSON_NUMBER);
        for (int i = 0; i < numbers.size() / 2; ++i) {
            Person person = persons.get(numbers.get(i));
            Integer tmpValue = person.getLabelMap().get("Capacity");
            tmpValue += 1;
            if (tmpValue >= SystemConfig.CHANGE_MAX) {
                tmpValue = SystemConfig.CHANGE_MAX;
            }
            person.getLabelMap().put("Capacity", tmpValue);
        }

        for (int i = numbers.size() / 2; i < numbers.size(); ++i) {
            Person person = persons.get(numbers.get(i));
            Integer tmpValue = person.getLabelMap().get("Capacity");
            tmpValue -= 1;
            if (tmpValue <= SystemConfig.CHANGE_MIN) {
                tmpValue = SystemConfig.CHANGE_MIN;
            }
            person.getLabelMap().put("Capacity", tmpValue);
        }
    }

    private void excelPersonValue(String labelName, String title, HashMap<Integer, List<String>> labelValues) {
        excelFile.getSheet("person-" + labelName);
        //set title
        List<String> values = Arrays.asList(title.split(SystemConfig.SEPARATION_CHARACTER));
        excelFile.setRowValue(values);
        //set content
        for (int i = 0; i < SystemConfig.TOTAL_PERSON_NUMBER; ++i) {
            excelFile.setRowValue(labelValues.get(i + 1));
        }
    }
}
